<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQ" inline>
        <el-form-item label="流水号">
          <el-input v-model="listQ.serialNo" placeholder="流水号"></el-input>
        </el-form-item>
        <el-form-item label="计划号">
          <el-input v-model="listQ.planNo" placeholder="计划号"></el-input>
        </el-form-item>
        <el-form-item label="物料">
          <el-input v-model="listQ.material" placeholder="物料"></el-input>
        </el-form-item>
        <el-form-item label="BOM">
          <el-input v-model="listQ.bom" placeholder="BOM"></el-input>
        </el-form-item>
        <el-form-item label="生产线">
          <el-input
            v-model="listQ.productionLine"
            placeholder="生产线"
          ></el-input>
        </el-form-item>
        <el-form-item label="工艺">
          <el-input v-model="listQ.process" placeholder="工艺"></el-input>
        </el-form-item>
        <el-form-item label="项目">
          <el-input v-model="listQ.project" placeholder="项目"></el-input>
        </el-form-item>
        <el-form-item label="生产日期">
          <el-date-picker
            v-model="listQ.productionDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交货日期">
          <el-date-picker
            v-model="listQ.deliveryDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="listQ.publishStatus" placeholder="状态"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="handleAdd">新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }" width="200px">
          <el-button type="primary" @click="handleEdit(row)" size="mini"
            >修改</el-button
          >
          <el-button type="danger" @click="handleDel(row.id)" size="mini"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog :title="title" center :visible.sync="visible" @close="close">
      <el-form inline ref="listERef" :model="listE">
        <el-form-item label="计划号" prop="planNo" required>
          <el-input
            :disabled="handleDisabled()"
            v-model="listE.planNo"
            placeholder="计划号"
          ></el-input>
        </el-form-item>
        <el-form-item label="项目" prop="project">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.project"
            placeholder="项目"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品" prop="product">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.product"
            placeholder="产品"
          ></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="productSpecification">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.productSpecification"
            placeholder="规格"
          ></el-input>
        </el-form-item>
        <el-form-item label="型号" prop="productModel">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.productModel"
            placeholder="型号"
          ></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="productUnit">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.productUnit"
            placeholder="单位"
          ></el-input>
        </el-form-item>
        <el-form-item label="BOM" prop="bom" required>
          <el-input
            :disabled="handleDisabled()"
            v-model="listE.bom"
            placeholder="BOM"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产线" prop="productionLine" required>
          <el-input
            :disabled="handleDisabled()"
            v-model="listE.productionLine"
            placeholder="生产线"
          ></el-input>
        </el-form-item>
        <el-form-item label="工艺" prop="process" required>
          <el-input
            :disabled="handleDisabled()"
            v-model="listE.process"
            placeholder="工艺"
          ></el-input>
        </el-form-item>
        <el-form-item label="待产数量" prop="waitingAmount" required>
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.waitingAmount"
            placeholder="待产数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="已产数量" prop="producedAmount">
          <el-input
            :disabled="handleDisabled(true)"
            v-model="listE.producedAmount"
            placeholder="已产数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker
            :disabled="handleDisabled()"
            type="datetime"
            v-model="listE.productionDate"
            placeholder="生产日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="交货日期" prop="deliveryDate">
          <el-date-picker
            :disabled="handleDisabled(true)"
            type="datetime"
            v-model="listE.deliveryDate"
            placeholder="交货日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="publishStatus">
          <el-select v-model="listE.publishStatus">
            <el-option label="未发布" value="未发布"></el-option>
            <el-option label="已发布" value="已发布"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :disabled="handleDisabled()"
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listE.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="listE.detailList">
        <el-table-column
          v-for="item in detailListC"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
        ></el-table-column>
      </el-table>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getProcessModelingList, editProcessModelingList, addProcessModelingList, delProcessModelingList } from '@/api_zc/mes/process-modeling'
import { getProductScheduleList, editProductSchedule, addProductSchedule } from '@/api_zc/mes/product-schedule'
import cloneDeep from 'lodash/cloneDeep'
export default {
  mounted () {
    this.getList()
  },
  data () {
    return {
      list: [],
      keyword: '',
      listQ: {
        serialNo: '',
        planNo: '',
        material: '',
        bom: '',
        productionLine: '',
        process: '',
        project: '',
        productionDate: [],
        productionStartDate: '',
        productionEndDate: '',
        deliveryStartDate: '',
        deliveryEndDate: '',
        deliveryDate: [],
        publishStatus: ''
      },
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '流水号',
          prop: 'serialNo'
        },
        {
          label: '计划好',
          prop: 'planNo'
        },
        {
          label: '产品',
          prop: 'product'
        },
        {
          label: '型号',
          prop: 'productModel'
        },
        {
          label: '规格',
          prop: 'productSpecification'
        },
        {
          label: '单位',
          prop: 'productUnit'
        },
        {
          label: 'BOM',
          prop: 'bom'
        },
        {
          label: '生产线',
          prop: 'productionLine'
        },
        {
          label: '工艺',
          prop: 'process'
        },
        {
          label: '项目',
          prop: 'project'
        },
        {
          label: '待产数量',
          prop: 'waitingAmount'
        },
        {
          label: '已产数量',
          prop: 'producedAmount'
        },
        {
          label: '生产日期',
          prop: 'productionDate'
        },
        {
          label: '交货日期',
          prop: 'deliveryDate'
        },
        {
          label: '发布状态',
          prop: 'publishStatus'
        },
        {
          label: '领料状态',
          prop: 'materialStatus'
        },
      ],
      listE: {
        bom: null,
        deliveryDate: null,
        description: null,
        name: null,
        planNo: null,
        powerConsumption: null,
        process: null,
        producedAmount: null,
        product: null,
        productModel: null,
        productSpecification: null,
        productUnit: null,
        productionDate: null,
        productionLine: null,
        project: null,
        publishStatus: null,
        remark: null,
        waitingAmount: null,
      },
      detailListC: [
        {
          label: '物料',
          prop: 'material'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '需求数量',
          prop: 'demandAmount'
        },
        {
          label: '已用数量',
          prop: 'usedAmount'
        },
        {
          label: '剩余数',
          prop: 'remainAmount'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      visible: false,
      title: '',
      isAdd: false,
    }
  },
  methods: {
    async getList () {
      const { data: { content, total } } = await getProductScheduleList({ ...this.listQ, keyword: this.keyword, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    handleDisabled (flag) {
      if (this.isAdd) {
        return false
      }
      if (this.listE.publishStatus == '已发布' || (this.listE.publishStatus == '未发布' && flag)) {
        return true
      }
      return false
    },
    handleReset () {
      Object.assign(this.listQ, this.$options.data().listQ)
      this.getList()
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    close () {
      this.visible = false
    },
    handleEdit (row) {

      this.visible = true
      this.title = "修改工艺建模"
      this.isAdd = false
      this.$nextTick(() => {
        this.listE = cloneDeep(row)
      })
    },
    handleAdd () {
      this.visible = true
      this.title = "修改工艺建模"
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs.listERef.resetFields()
      })
    },
    async handleDel (id) {
      await delProcessModelingList({ ids: [id] })
      this.$message.success('删除成功')
      this.getList()
    },
    submit () {
      this.listE.deliveryDate = new Date(this.listE.deliveryDate)
      this.listE.productionDate = new Date(this.listE.productionDate)
      this.$refs.listERef.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请填写必要字段')
        }
        if (this.isAdd) {
          await addProductSchedule(this.listE)
          this.$message.success('添加成功')
        } else {
          await editProductSchedule(this.listE)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss">
</style>